package com.example.sw.myapp1005;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.sw.myapp1005.Bean.AlamItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private List<AlamItem> itemList= new ArrayList<>();
    private NaoZhongAdapter2 adapter;
    //当ListView的子布局含有按钮时，会抢夺父布局的点击焦点
    //需要在item里Switch里加入focusable=flase
    //清单文件 改lable=闹钟





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//实例化控件
        setListener();//设置监听




    }

    private void setListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int postion, long l) {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("是否删除闹钟？")
                                .setMessage("点击确定删除闹钟？")
                                .setIcon(R.mipmap.ic_launcher)
//                                .setPositiveButton("确定", null)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        itemList.remove(postion);
                                        //更新视图 数据源重新排
                                        adapter.notifyDataSetInvalidated();
                                    }
                                })

//                                .setNegativeButton("取消", null);
                                .setNegativeButton("取消", null);

                builder.create().show();


            }

        });



    }

    private void initView() {

        lv=findViewById(R.id.lv);
        //填充数据源 无参
        AlamItem a=new AlamItem();
        a.setTime("12:01");
        a.setIschecked(false);
        itemList.add(a);
        //有参
        AlamItem a1=new AlamItem("12:10",false);
        itemList.add(a1);
        AlamItem a2=new AlamItem("12:12",false);
        itemList.add(a2);
        adapter= new NaoZhongAdapter2(MainActivity.this,R.layout.list_item,itemList);
        lv.setAdapter(adapter);



    }
}
